﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoodTrader.GTDownload
{
    public class CpOptionManager
    {
        // 최근월물 가격 목록
        private List<string> mRMPriceList = new List<string>();
        public List<string> RMPriceList
        {
            get { return mRMPriceList; }
            set { mRMPriceList = value; }
        }

        public void MakeRecentMonthPriceList(int aDep)
        {
            mRMPriceList.Clear();

            int callMonth = 4;
            int putMonth = 4;
            string preMonth = "-1";
            for (int i = 0; i < mCodeArray.Length; i++)
            {
                string opCode = mCodeArray[i];
                string sideCode = opCode.Substring(0, 1);
                string monthCode = opCode.Substring(opCode.Length - 4, 1);
                string priceCode = opCode.Substring(opCode.Length - 3, 3);
                if (sideCode == "2")
                {
                    if (monthCode != preMonth)
                    {
                        callMonth--;
                    }
                    if (callMonth == (4 - aDep))
                    {
                        mRMPriceList.Add(opCode);
                    }
                    preMonth = monthCode;
                }
                else
                {
                    if (monthCode != preMonth)
                    {
                        putMonth++;
                    }
                    if (putMonth == (4 + aDep))
                    {
                        mRMPriceList.Add(opCode);
                    }
                    preMonth = monthCode;
                }
            }
        }

        private CpOptionATM mOptionATMDownloader = new CpOptionATM();
        public GoodTrader.GTDownload.CpOptionATM OptionATMDownloader
        {
            get { return mOptionATMDownloader; }
            set { mOptionATMDownloader = value; }
        }
        public void DownloadATM()
        {
            mOptionATMDownloader.Download();
        }
 
        private string[] mCodeArray = null;
        public string[] CodeArray
        {
            get { return mCodeArray; }
            set { mCodeArray = value; }
        }

        private List<string> mPriceCodeList = new List<string>();
        public List<string> PriceCodeList
        {
            get { return mPriceCodeList; }
            set { mPriceCodeList = value; }
        }

        CPUTILLib.CpOptionCode m_CpOptionCode = new CPUTILLib.CpOptionCode();

        public CpOptionManager()
        {
            int codeCount = m_CpOptionCode.GetCount();
            mCodeArray = new string[codeCount];
            for (short i = 0; i < codeCount; i++)
            {
                mCodeArray[i] = m_CpOptionCode.GetData(0, i).ToString();
            }

            MakePriceCodeList(mCodeArray);
        }

        private void MakePriceCodeList(string[] aOpCodeArray)
        {
            string oCode = aOpCodeArray[0];
            string oldMonthCode = oCode.Substring(oCode.Length - 4, 1);
            for (short i = 0; i < aOpCodeArray.Length; i++)
            {
                oCode = aOpCodeArray[i];
                string monthCode = oCode.Substring(oCode.Length - 4, 1);
                if (oldMonthCode == monthCode)
                {
                    string priceCode = oCode.Substring(oCode.Length - 3, 3);
                    mPriceCodeList.Add(priceCode);
                }
                else
                    break;
            }
        }

        public int FindPriceIndex(string aPriceCode)
        {
            for (int i = 0; i < mPriceCodeList.Count; i++)
            {
                string opCode = mPriceCodeList[i];
                if (opCode == aPriceCode)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}
